package com.hundsun.cprs.yyt.web.action.system;

import com.alibaba.fastjson.JSONObject;
import com.hundsun.cprs.yyt.common.enums.EnumAreaCode;
import com.hundsun.cprs.yyt.service.report.domain.DaliyClinchSum;
import com.hundsun.cprs.yyt.service.report.service.DaliyClinchSumService;
import com.hundsun.cprs.yyt.util.DateUtil;
import com.hundsun.cprs.yyt.util.MoneyUtils;
import com.hundsun.cprs.yyt.web.action.BaseAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@Controller
public class ChartsAction extends BaseAction {

    private int time = 10;

    @Autowired
    private DaliyClinchSumService daliyClinchSumService;

    @RequestMapping("system/welcome/charts.htm")
    public String chartsList(){
        return "system/welcome/charts";
    }

    @RequestMapping("system/welcome/getData.json")
    @ResponseBody
    public void getData(HttpServletResponse response){
        JSONObject json = new JSONObject();
        //获取前十天的日期
        handleDate(json);
        //获取前十天的交易金额汇总和交易数量汇总
        handleTradeMoneyAndAmountSum(json);
        try {
            response.getWriter().print(json);
        } catch (Exception e) {
            log.error(e, e);
        }
    }

    public void handleDate(JSONObject json){
        List<String> dateStringList = new ArrayList<>();
        Date day = new Date();
        for (int i=0;i<time;i++){
            day = DateUtil.getPreviousDay(day,1);
            dateStringList.add(DateUtil.convertDateToString("yyyy-MM-dd",day));
        }
        //集合逆序
        Collections.reverse(dateStringList);
        json.put("dateArray",dateStringList.toArray());
    }

    public void handleTradeMoneyAndAmountSum(JSONObject json){
        List<Double> retileTradeMoneyList = new ArrayList<>();//零售区日交易金额汇总
        List<Double> wholeTradeMoneyList = new ArrayList<>();//批发区日交易金额汇总
        List<Double> retileTradeAmountList = new ArrayList<>();//零售区日交易数量汇总
        List<Double> wholeTradeAmountList = new ArrayList<>();//批发区日交易数量汇总
        //获取零售区近十天日成交统计数据
        List<DaliyClinchSum> retailAreaDaliyClinchSums = daliyClinchSumService.getRecentRecord(EnumAreaCode.RETAIL_AREA.getCode(),time);
        formData(retailAreaDaliyClinchSums,retileTradeMoneyList,retileTradeAmountList);
        //获取批发区近十天日成交统计数据
        List<DaliyClinchSum> wholesaleAreaDaliyClinchSums = daliyClinchSumService.getRecentRecord(EnumAreaCode.WHOLESALE_AREA.getCode(),time);
        formData(wholesaleAreaDaliyClinchSums,wholeTradeMoneyList,wholeTradeAmountList);
        //集合逆序
        Collections.reverse(retileTradeMoneyList);
        Collections.reverse(wholeTradeMoneyList);
        Collections.reverse(retileTradeAmountList);
        Collections.reverse(wholeTradeAmountList);
        //设置返回数据
        json.put("retileColumDataArray",retileTradeMoneyList.toArray());
        json.put("wholeColumDataArray",wholeTradeMoneyList.toArray());
        json.put("retileLineDataArray",retileTradeAmountList.toArray());
        json.put("wholeLineDataArray",wholeTradeAmountList.toArray());
        json.put("time",time);
    }

    public void formData(List<DaliyClinchSum> retailAreaDaliyClinchSums,List<Double> retileTradeMoneyList,List<Double> retileTradeAmountList){
        if (retailAreaDaliyClinchSums!=null && retailAreaDaliyClinchSums.size()>=time){
            for (DaliyClinchSum daliyClinchSum:retailAreaDaliyClinchSums){
                retileTradeMoneyList.add(Double.parseDouble(MoneyUtils.getMoneyFormatFen(daliyClinchSum.getClinchMoneySum())));
                retileTradeAmountList.add(daliyClinchSum.getClinchAmountSum().doubleValue());
            }
        }else if (retailAreaDaliyClinchSums!=null && retailAreaDaliyClinchSums.size()< time){
            for (DaliyClinchSum daliyClinchSum:retailAreaDaliyClinchSums){
                retileTradeMoneyList.add(Double.parseDouble(MoneyUtils.getMoneyFormatFen(daliyClinchSum.getClinchMoneySum())));
                retileTradeAmountList.add(daliyClinchSum.getClinchAmountSum().doubleValue());
            }
            for (int i= 0;i<time-retailAreaDaliyClinchSums.size();i++){
                retileTradeMoneyList.add(new Double(0));
                retileTradeAmountList.add(new Double(0));
            }
        }else{
            for (int i= 0;i<time;i++){
                retileTradeMoneyList.add(new Double(0));
                retileTradeAmountList.add(new Double(0));
            }
        }
    }
}


